﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KaraokeManagementDTO;
using KaraokeManagementBUS;

namespace KaraokeManagement
{
    public partial class frmPhieuNhapKho : DevComponents.DotNetBar.Office2007Form
    {
        public frmPhieuNhapKho()
        {
            InitializeComponent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            HangHoaDTO dto = new HangHoaDTO();
            dto.TenHangHoa = txtTenHangHoa.Text;
            dto.SoLuongTon = int.Parse(txtSoLuongTon.Text);
            dto.NhomHangHoa_ID = int.Parse(cbNhomHang.SelectedValue.ToString());
            dto.DonViTinh_ID = int.Parse(cbDonViTinh.SelectedValue.ToString());
            dto.DonGia = double.Parse(txtDonGia.Text);
            dto.MoTaHangHoa = txtMoTaHangHoa.Text;
            dto.GiamGia = double.Parse(txtGiamGia.Text);
            
            if (ucQuanLyHangHoa.staticMaHangHoa != -1)
            {
                dto.ID = ucQuanLyHangHoa.staticMaHangHoa;

                if (new HangHoaBUS().capNhatHangHoa(dto))
                {
                    MessageBox.Show("Cập nhật thành công !", "Karaoke ABC");
                    ucQuanLyHangHoa.staticMaHangHoa = -1;
                    this.Close();
                    return;
                }
            }

            if (new HangHoaBUS().themHangHoa(dto))
            {
                MessageBox.Show("Thêm thành công 1 hàng hóa !", "Karaoke ABC");
                this.Close();
            }
        }

        private void frmPhieuNhapKho_Load(object sender, EventArgs e)
        {
            initDonViTinh();
            initNhomHang();

            if (ucQuanLyHangHoa.staticMaHangHoa != -1)
            {
                HangHoaDTO dto = new HangHoaDTO();
                dto = new HangHoaBUS().layThongTinHangHoa(ucQuanLyHangHoa.staticMaHangHoa);

                txtTenHangHoa.Text = dto.TenHangHoa;
                txtDonGia.Text = dto.DonGia.ToString();
                txtMoTaHangHoa.Text = dto.MoTaHangHoa;
                txtSoLuongTon.Text = dto.SoLuongTon.ToString();
                txtGiamGia.Text = dto.GiamGia.ToString();
                cbDonViTinh.SelectedValue = dto.DonViTinh_ID;
                cbNhomHang.SelectedValue = dto.NhomHangHoa_ID;
            }
        }

        public void initNhomHang()
        {
            cbNhomHang.DataSource = new NhomHangHoaBUS().layDanhSachNhomHangHoa();
            cbNhomHang.DisplayMember = "TenNhomHangHoa";
            cbNhomHang.ValueMember = "ID";
        }

        public void initDonViTinh()
        {
            cbDonViTinh.DataSource = new DonViTinhBUS().layDanhSachDonViTinh();
            cbDonViTinh.DisplayMember = "TenDonViTinh";
            cbDonViTinh.ValueMember = "ID";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
